<template>
    <div class="login-container">
        <div class="mt-100">
            <el-form :model="loginForm" :rules="loginRules" auto-complete="on" class="login-form" label-position="left"
                ref="loginForm">
                <div class="title-container pt-20">
                    <h3 class="title">{{ config.site.name }}</h3>
                </div>
                <el-form-item prop="account">
                    <el-input auto-complete="on" name="account" placeholder="请输入您的账号" ref="account" tabindex="1" type="text"
                        v-model="loginForm.account" />
                </el-form-item>
                <el-form-item prop="password">
                    <el-input :key="passwordType" :type="passwordType" @keyup.enter.native="handleLogin" auto-complete="on"
                        name="password" placeholder="请输入您的密码" ref="password" tabindex="2" v-model="loginForm.password" />
                    <span @click="showPwd" class="show-pwd pt-4">
                        <!-- <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" /> -->
                        <iconfont :icon="passwordType === 'password' ? 'icon-yanjing-biyan' : 'icon-yanjing'" size="20">
                        </iconfont>
                    </span>
                </el-form-item>
                <el-form-item prop="catpcha">
                    <div class="flex flex-1">
                        <el-input auto-complete="on" maxlength="4" name="catpcha" placeholder="请输入验证码" ref="catpcha"
                            tabindex="1" type="text" v-model="loginForm.catpcha" />
                        <a @click="getCaptchat()" href="javascript:void(0)" v-if="captcha_url">
                            <el-image :src="captcha_url" class="pl-5" style="height:32px;border-radius:5px"></el-image>
                        </a>
                    </div>
                </el-form-item>
                <el-button :loading="loading" @click.native.prevent="handleLogin" @keyup.enter.native="handleLogin"
                    style="width:100%;margin-bottom:30px;" type="primary">登录</el-button>
            </el-form>
        </div>

        <div class="pt-10 pb-10 bottom-location color-fff font-13">
            <div>技术支持@广西昊文网络科技有限公司</div>
            <div class="pt-4">
                <a class="pr-5" href>技术咨询</a>
                <a class="pl-5" href>申请加入</a>
            </div>
        </div>
    </div>
</template>

<script>
import config from '../../utils/config'
export default {
    name: 'Login',
    data() {
        const validateUsername = (rule, value, callback) => {
            if (value.length < 2) {
                callback(new Error('请输入您的用户名'))
            } else {
                callback()
            }
        }
        const validatePassword = (rule, value, callback) => {
            if (value.length < 6) {
                callback(new Error('密码长度不正确，至少6位'))
            } else {
                callback()
            }
        }
        const validateCatpcha = (rule, value, callback) => {
            if (value.length != 4) {
                callback(new Error('验证码长度错误'))
            } else {
                callback()
            }
        }
        return {
            loginForm: {
                account: '', //账号
                password: '',
                catpcha: '', //验证码
                uuid: '',
            },
            loginRules: {
                account: [{ required: true, trigger: 'blur', validator: validateUsername }],
                password: [{ required: true, trigger: 'blur', validator: validatePassword }],
                catpcha: [{ required: true, trigger: 'blur', validator: validateCatpcha }],
            },
            loading: false,
            passwordType: 'password',
            redirect: undefined,
            captcha_url: '',
            config: config,
        }
    },
    watch: {
        $route: {
            handler: function (route) {
                this.redirect = route.query && route.query.redirect
            },
            immediate: true,
        },
    },
    mounted() {
        this.enterKeyup()
        this.getCaptchat()
    },
    destroyed() {
        // 销毁enter事件
        this.enterKeyupDestroyed()
    },
    methods: {
        enterKey(event) {
            const componentName = this.$options.name
            if (componentName == 'Login') {
                const code = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode
                if (code == 13) {
                    this.handleLogin()
                }
            }
        },
        enterKeyupDestroyed() {
            document.removeEventListener('keyup', this.enterKey)
        },
        enterKeyup() {
            document.addEventListener('keyup', this.enterKey)
        },
        showPwd() {
            if (this.passwordType === 'password') {
                this.passwordType = ''
            } else {
                this.passwordType = 'password'
            }
            this.$nextTick(() => {
                this.$refs.password.focus()
            })
        },
        getCaptchat() {
            this.$request.get('/auth/captcha').then((res) => {
                this.captcha_url = res.catpcha
                this.loginForm.uuid = res.client
            })
        },
        handleLogin() {
            this.$refs.loginForm.validate((valid) => {
                if (valid) {
                    this.loading = true
                    this.$store
                        .dispatch('user/login', this.loginForm)
                        .then((res) => {
                            this.$router.push({ path: this.redirect || '/' })
                            this.loading = false
                        })
                        .catch((res) => {
                            this.getCaptchat()
                            this.loading = false
                        })
                } else {
                    console.log('error submit!!')
                    return false
                }
            })
        },
    },
}
</script>
<style lang="scss" scoped>
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
    min-height: 100%;
    // width: 100%;
    background-image: url('../../assets/loginbj.png');
    background-repeat: no-repeat;
    background-size: cover;
    background-attachment: fixed;
    overflow: hidden;

    .login-form {
        position: relative;
        width: 360px;
        max-width: 100%;
        // padding: 160px 35px 0;
        // margin-top: 200px;
        margin: 0 auto;
        overflow: hidden;
        padding: 10px 20px;
        border-radius: 10px;
        background: #fff;
    }

    .tips {
        font-size: 14px;
        color: #fff;
        margin-bottom: 10px;

        span {
            &:first-of-type {
                margin-right: 16px;
            }
        }
    }

    .svg-container {
        padding: 6px 5px 6px 15px;
        color: $dark_gray;
        vertical-align: middle;
        width: 30px;
        display: inline-block;
    }

    .title-container {
        position: relative;

        .title {
            font-size: 20px;
            color: #333;
            margin: 0px auto 40px auto;
            text-align: center;
            font-weight: bold;
        }
    }

    .show-pwd {
        position: absolute;
        right: 10px;
        font-size: 16px;
        color: $dark_gray;
        cursor: pointer;
        user-select: none;
    }
}

.bottom-location {
    position: absolute;
    bottom: 0;
    text-align: center;
    width: 100%;
}

.card {}

/deep/.el-image__inner {
    border-radius: 5px;
}</style>
